<?php 
	namespace Admin\Controller;
	use Think\Controller;

	class OrderController extends CommonController{
		public function index()
		{
			$orderModel=D('order_list');
			//总条数
			$total=$orderModel->getCount();
			//总页数
			$page=new \Think\Page($total,3);
			$page->setConfig('header','总页数');
			$page->setConfig('prev','上一页');
			$page->setConfig('next','下一页');
			//获取当前页
			$p=$page->nowPage;
			//echo $p;
			//exit;
			$page_str=$page->show();
			$limit=$page->firstRow.','.$page->listRows;

			$list=$orderModel->getAll($limit);
			//var_dump($list);exit;
			$this->assign('page',$page_str);
			$this->assign('p',$p);
			$this->assign('list',$list);
			$this->display();
		}
	//发货功能
		public function fa()
		{
			//接受当前页
			$p=$_GET['p'];

			$id=$_GET['id'];
			//查询数据库
			$sql="select `is_pay`,`is_delivery` from `order_list` where id=$id";
			$model=M('order_list');
			$list=$model->query($sql)[0];
			//若未付款,不能发货
			if ($list['is_pay']) 
			{
				$data['is_delivery']=1;
				$where['id']=$id;
				$res=$model->where($where)->save($data);
				//var_dump($res);
				if ($res) 
				{
					$this->redirect('Order/index',['p'=>$p]);
					return false;
				}
			}else{
				$this->error('买家还木有付款呢');
				return false;
			}
		}
	//后台订单删除
		public function del()
		{
			$id=$_GET['id'];
			//echo $id;exit;
			//查询数据库
			$sql="select `is_delivery` from `order_list` where id=$id";
			$model=M('order_list');
			$list=$model->query($sql)[0];
			//var_dump($list);exit;
			if ($list['is_delivery']!=1) 
			{
				$where['id']=$id;
				$res=$model->where($where)->delete();
				$this->redirect('Order/index');
				//$this->success('删除成功',U('Order/index'),3);
				return false;
			}else{
				$this->error('货物在运输途中，不能删除订单!');
				return false;
			}
		}
//后台订单详情
	public function detail()
	{
		$id=$_GET['id'];
		//echo $id;exit;
		//查询数据
		$order_model=D('order_info');

		//总条数
		$total=$order_model->getCount($id);
		//echo $total;exit;
		//总页数
		$page=new \Think\Page($total,1);
		$page->setConfig('header','总页数');
		$page->setConfig('prev','上一页');
		$page->setConfig('next','下一页');
		//获取当前页
		$p=$page->nowPage;

		$page_str=$page->show();
		//echo $page_str;exit;
		$limit=$page->firstRow.','.$page->listRows;

		$goods_list=$order_model->getAll($id,$limit);
		foreach ($goods_list as $ke => $val) 
		{
			$goods_list[$ke]['giname']='/Public/Uploads/'.$this->sub($val['giname']).'80_'.$val['giname'];
			$goods_list[$ke]['gtime']=date('Y年m月d日',$val['gtime']);
		}

		//发送数据
		$this->assign('page',$page_str);
		$this->assign('p',$p);
		$this->assign('goods_list',$goods_list);
		//加载模板
		$this->display();
		}

		private function sub($str)
		{
			return substr($str, 0,4).'/'.substr($str, 4,2).'/'.substr($str, 6,2).'/';
		}
	}

?>